import java.util.Arrays;

public class Array {
    public static void main(String[] args) {
        /*
        int[] array = new int[]{1, 2, 3, 4, 5, 6};
        int index = binarySearch(array,7);
        if(index == -1) {
            System.out.println("没有找到你要找的数字！");
        }else {
            System.out.println("找到了你要找的数字！是：" + index);
        }
        System.out.println(myString(array));
        System.out.println(Arrays.toString(array));
        */

        int[] array = new int[]{1, 21, 31, 3, 5};
        transform(array);
        System.out.println(Arrays.toString(array));
        bubbleSort(array);
        System.out.println("冒泡排序后的数组为：" + Arrays.toString(array));
        int[] array2 = new int[]{1,2,3,4,5,6};
        func(array2);
        System.out.println(Arrays.toString(array2));

        int[] array3 = new int[]{2,7,11,15};
        int[] ret = twoindex(array3,18);
        System.out.println(Arrays.toString(ret));

    }


    //冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = true;
                }
            }
            if (flg == false) {
                break;
            }
        }
    }

    //二分查找
    public static int binarySearch(int[] array, int key) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (mid < key) {
                left = mid + 1;
            } else if (mid == key) {
                return mid;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static String myString(int[] array) {
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            if (i < array.length - 1) {
                ret += array[i] + ", ";
            } else {
                ret += array[i];
            }
        }
        ret += "]";
        return ret;
    }

    public static void transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }

    //交换位置
    private static void swap(int array[], int i, int j) {
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }

    public static void func(int[] array) {
        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            while (i < j && array[i] % 2 != 0) {
                i++;
            }
            while (i < j && array[j] % 2 == 0) {
                j--;
            }
            swap(array, i, j);
        }
    }

    public static int[] twoindex(int[] array,int target) {
        int[] tmp = {-1,1};
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if(array[i] + array[j] == target) {
                    tmp[0] = array[i];
                    tmp[1] = array[j];
                }
            }
        }
        return tmp;
    }
}
